<?php

// SignWav includes
require_once 'SignWav.php';
require_once 'Logger.php';
require_once 'Arguments.php';
require_once 'File.php';

// getid3 includes
require_once 'Externals/getid3/getid3.php';
require_once 'Externals/getid3/write.php';
require_once 'Externals/getid3/write.apetag.php';
require_once 'Externals/getid3/write.id3v1.php';
require_once 'Externals/getid3/write.id3v2.php';
require_once 'Externals/getid3/write.lyrics3.php';
require_once 'Externals/getid3/write.metaflac.php';
require_once 'Externals/getid3/write.real.php';
require_once 'Externals/getid3/write.vorbiscomment.php';

run($argv);

function run($argv)
{

	$arguments = new Arguments($argv);

	if ($arguments->getArgumentValue('Help', false)) {
		Arguments::printHelp();
		exit(0);
	}

	$logger = new Logger();
	if ($arguments->getArgumentValue('Verbose', false)) {
		$logger->setLogLevel(Logger::LevelDebug);
	} else {
		$logger->setLogLevel(Logger::LevelDisplay);
	}

	$source = $arguments->getArgumentValue('Source', '');
	if (strlen($source) < 1) {
		$logger->display('Missing argument! -' . $arguments->getArgumentByName('Source') . ' not supplied');
		Arguments::printHelp();
		exit(0);
	}
	
	$signWav = new SignWav($logger, $source);

	$file = $arguments->getArgumentValue('Duplicate File Name', false);
	if ($file !== false) {
		$signWav->collectFiles();
		$logger->display('looking for duplicate file names...');
		$duplicates = $signWav->duplicateFileNames();
		if (count($duplicates) > 0) {
			$logger->display("\nwriting duplicates to file...");
			$signWav->outputToFile($file, 'file name', $duplicates);
		} else {
			$logger->display("No duplicate file names detected");
		}
	}

	$file = $arguments->getArgumentValue('Duplicate File Hash', false);
	if ($file !== false) {
		$signWav->collectFiles();
		$logger->display('looking for duplicate file hashes...');
		$duplicates = $signWav->duplicateMd5Hashes();
		if (count($duplicates) > 0) {
			$logger->display("\nwriting duplicates to file...");
			$signWav->outputToFile($file, 'file hash', $duplicates);
		} else {
			$logger->display("No duplicate file hashes detected");
		}
	}

	$file = $arguments->getArgumentValue('Duplicate Audio Hash', false);
	if ($file !== false) {
		$signWav->collectFiles();
		$logger->display('looking for duplicate audio hashes...');
		$duplicates = $signWav->duplicateAudioOnlyMd5Hashes();
		if (count($duplicates) > 0) {
			$logger->display("\nwriting duplicates to file...");
			$signWav->outputToFile($file, 'audio only hash', $duplicates);
		} else {
			$logger->display("No duplicate audio hashes detected");
		}
	}

	if ($arguments->getArgumentValue('Reorganize', false)) {
		$destination = $arguments->getArgumentValue('Destination', '');
		if (strlen($destination) < 1) {
			$logger->display('Missing argument! -' . $arguments->getArgumentByName('Destination') . ' not supplied');
			Arguments::printHelp();
			exit(0);
		}
		
		$signWav->collectFiles();
		$logger->display('Ok, merging media...');

		$success = $signWav->reorganizeMedia($destination);
		if (!$success) {
			$logger->display("Wow, that didn't work!");
		}

	}

	File::cleanUp();

	$logger->display("\n\nSignWav is done!\n\n");
}
// ***************************************************************************************

function makeFileTags()
{
	$getID3 = new getID3;
	$info = $getID3->analyze('nateDead.mp3');
	var_dump($info);

	$info['tags']['id3v1']['title'] = array('NATE WAS HERE');

	$tagWriter = new getid3_writetags();
	$tagWriter->filename = 'nateDead.mp3';
	$tagWriter->tagformats = array('id3v1');
	$tagWriter->tag_encoding = 'UTF-8';
	$tagWriter->tag_data = $info['tags']['id3v1'];
	//$tagWriter->tag_data = array(
	//	'title' => array('NATE WAS HERE'),
	//	'album' => "Nate's Album",
	//	'year' => "1977",
	//	'track' => 'trak 1',
	//	'genre' => array('peace', 'rock'),
	//	'artist' => array('nate')
	//);
	//var_dump($tagWriter->tag_data);
	$tagWriter->remove_other_tags = true;
	$tagWriter->WriteTags();
	print "done";
}